<?php
session_start();

$con = mysql_connect("127.0.0.1","root","");
  
if (!$con) {
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("yeoldeguilde", $con);

if (!isset($_GET['sort'])) {
	$_GET['sort'] = "name";
	$_GET['direction'] = "ASC";
}

if ($_GET['direction'] == "DESC") {
	$_GET['direction'] = "ASC";
	$oldSort = "DESC";
} else {
	$_GET['direction'] = "DESC";
	$oldSort = "ASC";
}

if (!isset($_SESSION['totalPages'])) {
	$_SESSION['totalPages'] = mysql_num_rows(mysql_query("SELECT id FROM members"));  
}
if (isset($_GET['page']) && ($_GET['page'] >= 1)) {
	$currentPage = $_GET['page'];
	$pageBack = $currentPage - 1;
} else {
	$currentPage = 0;
	$pageBack = 0;
}
	
	$pageForward = $currentPage + 1;
	if ($pageForward > $_SESSION['totalPages'])
		$pageForward = $_SESSION['totalPages'];

$pageIncrement = 50;

?>
<!DOCTYPE html> 
<html> 
	<head> 
	<title>Ye Olde Guilde Banke</title> 
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
	<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
	<script type="text/javascript" src="http://static.wowhead.com/widgets/power.js"></script>
	<style type="text/css">
.icon { width: 80px; }

</style>
</head> 
<body> 

<div data-role="page"  data-theme="a" >
	
	<div data-role="header">
	<h1>Ye Olde Guilde Members</h1>
	<a href="../../" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
			<div data-role="navbar">
		<ul>
			<li><a href="?sort=name&direction=<?php echo $_GET['direction']."\""; ?>>Name</a></li>
			<li><a href="?sort=level&direction=<?php echo $_GET['direction']."\""; ?>>Level</a></li>
			<li><a href="?sort=rank&direction=<?php echo $_GET['direction']."\""; ?>>Rank</a></li>
			<li><a href="?sort=class&direction=<?php echo $_GET['direction']."\"";?>>Class</a></li>
		</ul>
	</div><!-- /navbar -->
	</div><!-- /header -->



	
	<div data-role="content">
			<div class="content-primary">
			<ul data-role="listview">
	<?php
	
  
$result = mysql_query("SELECT * FROM members ORDER BY ".$_GET['sort']." ".$oldSort." LIMIT ".$currentPage * $pageIncrement.", ".$pageIncrement);

$lastChar = '-';
while($row = mysql_fetch_array($result)) {
	$currentName = $row['name'];

	if ($_GET['sort'] == "name") {
		$newChar = substr($currentName, 0, 1);
	} else if ($_GET['sort'] == "class") {
		$newChar = substr($row['class'], 0, 2);
	} else if ($_GET['sort'] == "level") {
		$newChar = substr($row['level'], 0, 2);
	} else {
		$newChar = $row['rank'];
	}
	
	
	if ($lastChar != $newChar) {
		$lastChar = $newChar;
		echo "<li data-role=\"list-divider\">".$newChar."</li>";
	}
	?>
				
				<div data-role="collapsible">
		<h3><?php 
			if ($row['rank'] === "Bar Wench") {
				echo "<font color=#1eff00>";
			} else if ($row['rank'] === "Barkeep") {
				echo "<font color=#0070dd>";
			} else {
				echo "<font>";
			}
			
				echo $currentName ?></font>
				 - <?php echo $row['level']; ?> <?php echo $row['class']; ?>
				</h3>
		<p>
		<fieldset data-role="controlgroup" data-type="horizontal">
		<legend>Rank:</legend>
     	<input disabled  type="radio" name="radio-choice-<?php echo $currentName; ?>1" id="radio-choice-<?php echo $currentName; ?>1" value="choice-<?php echo $currentName; ?>1" <?php if($row['rank'] == "Barkeep") echo "checked=\"checked\"";?> />
     	<label for="radio-choice-<?php echo $currentName; ?>1">Barkeep</label>

     	<input disabled  type="radio" name="radio-choice-<?php echo $currentName; ?>2" id="radio-choice-<?php echo $currentName; ?>2" value="choice-<?php echo $currentName; ?>2" <?php if($row['rank'] == "Bar Wench") echo "checked=\"checked\"";?> />
     	<label for="radio-choice-<?php echo $currentName; ?>2">Bar Wench</label>

     	<input disabled  type="radio" name="radio-choice-<?php echo $currentName; ?>3" id="radio-choice-<?php echo $currentName; ?>3" value="choice-<?php echo $currentName; ?>3" <?php if($row['rank'] == "Henchman") echo "checked=\"checked\"";?> />
     	<label for="radio-choice-<?php echo $currentName; ?>3">Henchman</label>

     	<input disabled  type="radio" name="radio-choice-<?php echo $currentName; ?>4" id="radio-choice-<?php echo $currentName; ?>4" value="choice-<?php echo $currentName; ?>4" <?php if($row['rank'] == "Drunkard") echo "checked=\"checked\"";?> />
     	<label for="radio-choice-<?php echo $currentName; ?>4">Drunkard</label>
		
		<input disabled  type="radio" name="radio-choice-<?php echo $currentName; ?>5" id="radio-choice-<?php echo $currentName; ?>5" value="choice-<?php echo $currentName; ?>5" <?php if($row['rank'] == "Peasant") echo "checked=\"checked\"";?> />
     	<label for="radio-choice-<?php echo $currentName; ?>5">Peasant</label>
		
		<input disabled  type="radio" name="radio-choice-<?php echo $currentName; ?>6" id="radio-choice-<?php echo $currentName; ?>6" value="choice-<?php echo $currentName; ?>6" <?php if($row['rank'] == "Sleep It Off") echo "checked=\"checked\"";?> />
     	<label for="radio-choice-<?php echo $currentName; ?>6">Sleep It Off</label>
</fieldset></p>
	</div>
			
	<?php
	}
	?>
			</ul>
			</div><!--/content-primary -->		
<?php
	/*
?>

<?php
}
	mysql_close($con);
  /*
$row = mysql_fetch_array($result);
$currentName = $row['banker'];
	echo "<div data-role=\"collapsible\">";
    echo "<h3>".$currentName."</h3>";
	//echo "<table>";
while($row = mysql_fetch_array($result)) {
	if ($row['banker'] != $currentName) {
		$currentName = $row['banker'];
		echo "</table>";
		echo "</div>";
		echo "<div data-role=\"collapsible\">";
		echo "<h3>".$currentName."</h3>";
		//echo "<table>";
	}
	echo "<input type=\"checkbox\" name=\"checkbox-".$row['id']."\" id=\"checkbox-".$row['id']."\" class=\"custom\" />";
	echo " <label for=\"checkbox-".$row['id']."\">";
	echo "<table>";
	echo "<tr>";
	echo "<td>";
	echo "<div align=\"right\">".$row['itemCount']."</div>";
	echo "</td>";
	echo "<td>x</td>";
	echo "<td>";
	echo "<a href=\"http://old.wowhead.com/item=".$row['itemID']."\"><font color=\"".$row['itemColor']."\">";
	echo $row['itemStr']."</font></a>"."<br>";
	echo "</td>";
	echo "<td>";
	
	echo "</td>";
	echo "</tr>";
	echo "</table>";
	echo "</label><br>";
	
	
	/*;<< mainItem[curItem].RGB[0] << mainItem[curItem].RGB[1] << mainItem[curItem].RGB[2] << mainItem[curItem].RGB[3] << mainItem[curItem].RGB[4] << mainItem[curItem].RGB[5] << '>';
	echo "<table style=\"float:left; border: 2px solid black;\"><tr><td><a href=\"http://old.wowhead.com/item=".$row['item_id']."\">";
	echo "<div  style=\"background-image: url(http://db.valkyrie-wow.com/images/icons/large/".$res['icon'].".jpg);  height: 65px; width: 65px; border: 2px solid black; float: left;  margin: 5px \">";
  echo "<span style=\"color: #FFFFFF; font-size:150%;\">".$row['item_count']."</span> </div></a></td></tr><tr><td><input type=\"checkbox\"/></td></td></table>";
 
  }
echo "</table>";
echo "</div>";
*/
	?>
		
		
  
		
	</div><!-- /content -->
<div data-role="footer"  data-id="foo1" data-position="fixed">		
	<div data-role="navbar">
		<ul>
			<li><a href="?page=<?php echo $pageBack;?>" >Back</a></li>
			<li><a href="index.php">Home</a></li>
			<li><a href="?page=<?php echo $pageForward;?>">Forward</a></li>
		</ul>
	</div><!-- /navbar -->
</div><!-- /footer -->
</div><!-- /page -->

</body>
</html>